package com.aixin.project.filter;

import com.aixin.project.data.*;
import com.aixin.project.handler.ExpertHandler;
import com.aixin.project.handler.UserHandler;
import com.aixin.project.util.RequestUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: Sufeng
 * Date: 12-4-22
 * Time: 上午11:37
 * To change this template use File | Settings | File Templates.
 */
public class UserSessionFilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        AxSession axsession = (AxSession) request.getSession().getAttribute(CommonConstants.USER_SESSION);
        if (axsession == null) {
            String u = RequestUtil.getCookie("u", request);
            if (StringUtils.trimToNull(u) != null) {
                String[] sarr = u.split("_");
                if (sarr.length == 2) {
                    if ("u".equals(sarr[0])) { //user cookie
                        long uid = NumberUtils.toLong(sarr[1], -1);
                        User user = UserHandler.find(uid);
                        if (user != null) {
                            AxSession axSession = new AxSession(UserType.USER, uid, true,RequestUtil.getGenseeUid(request,response));
                            request.getSession().setAttribute(CommonConstants.USER_SESSION, axSession);
                        }
                    } else if ("e".equals(sarr[0])) { //export cookie
                        long eid = NumberUtils.toLong(sarr[1], -1);
                        Expert expert = ExpertHandler.find(eid);
                        if (expert != null) {
                            AxSession axSession = new AxSession(UserType.EXPERT, eid, true,RequestUtil.getGenseeUid(request,response));
                            request.getSession().setAttribute(CommonConstants.USER_SESSION, axSession);
                        }
                    }
                }
            }
        }

        if(null == request.getSession().getAttribute(CommonConstants.USER_SESSION)){
           // request.setAttribute("ERROR","未登录用户禁止访问该页面！");
        	//request.getRequestDispatcher("/login").forward(request, response);
           request.getSession().setAttribute("MESSAGE","您还没有登录，请登录后操作");
           response.sendRedirect("/login");
        }else
        	filterChain.doFilter(request,response);
    }

    public void destroy() {
        //To change body of implemented methods use File | Settings | File Templates.
    }
}
